<?php

/**
 * HTML table of emedia items.
 * 
 * @copyright (c) 2012 University of Geneva
 * @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html
 * @author Laurent Opprecht laurent@opprecht.info
 */
class EMediaTable
{

    /**
     *
     * @return EMediaTable
     */
    public static function create()
    {
        return new self();
    }

    protected $table = null;

    public function __construct()
    {
        $table = new html_table();
        $table->attributes['class'] = 'generaltable mod_index';

        $strname = get_string('name');
        $strintro = get_string('moduleintro');
        $strlastmodified = get_string('lastmodified');
        $table->head = array($strname, $strintro, $strlastmodified);
        $table->align = array('left', 'left', 'left');

        $this->table = $table;
    }

    function display($course, $items)
    {
        $table = $this->table;
        $table->data = array();

        $modinfo = get_fast_modinfo($course);

        foreach ($items as $item)
        {
            $cm = $modinfo->cms[$item->coursemodule];
            $modified = '<span class="smallinfo">' . userdate($item->mtime) . "</span>";

            if ($cm->get_icon_url())
            {
                $icon = '<img src="' . $cm->get_icon_url() . '" class="activityicon" alt="' . get_string('modulename', $cm->modname) . '" /> ';
            }
            else
            {
                $icon = '';
            }

            $extra = empty($cm->extra) ? '' : $cm->extra;
            $class = $item->visible ? '' : 'class="dimmed"'; // hidden modules are dimmed
            $title = "<a $class $extra href=\"view.php?id=$cm->id\">" . $icon . format_string($item->title) . "</a>";
            if ($item->intro)
            {
                $description = format_module_intro('emedia', $item, $cm->id);
            }
            else
            {
                $description = $item->description;
            }
            $table->data[] = array($title, $description, $modified);
        }

        echo html_writer::table($table);

        $table->data = array();
    }

}